﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LongQuanWeb.App_Code;
using System.IO;

namespace LongQuanWeb.Admin
{
    public partial class Venture_Pioneer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((this.Session["username"] == null) || (this.Session["username"].ToString() == string.Empty))
            {
                base.Response.Write("<font color=#ff0000 style='FONT-SIZE: 12px'>对不起,您没足够权限访问此页!!</font><br>");
                base.Response.Write("<a href='Login.aspx' target=_top style='FONT-SIZE: 12px'>重新登陆</a><br>");
                base.Response.End();
            }
            if (!IsPostBack)
            {
                base.Response.Write("<script>parent.window.document.getElementById( 'lbl_title').innerHTML='创业先锋'; </script>");
                ShowImage();
            }
        }

        //显示图片
        private void ShowImage()
        {
            string strSql = "Select Top 12 ImagePath From [Advertising] where ImageFlag=1";
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];
            for (int i = 1; i <= dt.Rows.Count; i++)
            {
                System.Web.UI.HtmlControls.HtmlImage img = (System.Web.UI.HtmlControls.HtmlImage)FindControl("img" + i.ToString().Trim());
                img.Src = @"../Upload/Pioneer/" + dt.Rows[i - 1]["ImagePath"].ToString().Trim() + "";
            }
        }

        protected void btn_upload1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string Num = btn.ID.Replace("Button", "");
            System.Web.UI.HtmlControls.HtmlInputFile HIF = (System.Web.UI.HtmlControls.HtmlInputFile)FindControl("File" + Num.Trim());
            string strSql = "Select ImagePath From [Advertising] where ID=" + Num.Trim() + " and ImageFlag=1";
            DataTable dt = DbHelperSQL.Query(strSql).Tables[0];

            /// 1、没有上传的文件
            /// 2、类型不允许
            /// 3、大小超限
            /// 4、未知错误
            /// 0、上传成功。
            DotNet.Utilities.ImageUpload iu = new DotNet.Utilities.ImageUpload();
            iu.SavePath = @"~/Upload/Pioneer/";
            iu.FormFile = HIF;
            iu.IsCreateImg = false;
            iu.Upload();

            if (iu.Error == 1)
            {
                base.Response.Write("<script>alert('没有上传的文件');</script>");
                return;
            }

            if (iu.Error == 2)
            {
                base.Response.Write("<script>alert('上传失败，不支持该类型文件上传');</script>");
                return;
            }

            if (iu.Error == 3)
            {
                base.Response.Write("<script>alert('上传失败，图片过大');</script>");
                return;
            }

            if (iu.Error == 4)
            {
                base.Response.Write("<script>alert('上传失败，未知错误');</script>");
                return;
            }

            if (iu.Error == 0)
            {
                strSql = "Update [Advertising] set ImagePath='" + iu.OutFileName.Trim() + "' where ID=" + Num.Trim() + " and ImageFlag=1";
                if (DbHelperSQL.ExecuteSql(strSql) <= 0)
                {
                    //出错删除新传图片
                    string filepath = System.Web.HttpContext.Current.Server.MapPath(@"~/Upload/Pioneer/" + iu.OutFileName.Trim() + "");
                    if (File.Exists(filepath))//判断文件是不是存在           
                    {
                        File.Delete(filepath);
                    }
                    base.Response.Write("<script>alert('保存数据库出错');</script>");
                    return;
                }
                //保存成功后 删除旧图片
                if (dt != null)
                {
                    string filepath = System.Web.HttpContext.Current.Server.MapPath(@"~/Upload/Pioneer/" + dt.Rows[0]["ImagePath"].ToString().Trim() + "");
                    if (File.Exists( filepath))//判断文件是不是存在           
                    {
                        File.Delete(filepath);
                    }
                }
                ShowImage();
                base.Response.Write("<script>alert('上传成功');</script>");
                return;
            }

        }
    }
}
